package com.epam.task.web.util;

import com.epam.task.web.util.logging.LoggerUtil;
import org.apache.log4j.Logger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryption {

    private final LoggerUtil logger = new LoggerUtil(Logger.getLogger(Encryption.class.getName()));

    public String getHashCode(String value) {
        MessageDigest messageDigest = null;
        byte[] byteData = null;
        StringBuffer hash = new StringBuffer();

        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException exception) {
            logger.getExceptionTextFileLogger().error(exception);
        }
        messageDigest.update(value.getBytes());

        byteData = messageDigest.digest();

        hash = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            hash.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        return hash.toString();
    }

}

